package com.appcenter.application.service;

import com.appcenter.application.dto.orgs.OrgDto;
import com.appcenter.application.expand.ObjectFinal;
import com.appcenter.application.input.org.GetOrgsInput;
import com.appcenter.application.mapper.OrgsMapper;
import com.appcenter.application.model.Orgs;
import com.appcenter.application.output.org.GetOrgsOutput;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.unit.ObjectUnit;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrgService {
    @Autowired
    OrgsMapper orgsMapper;
    @Autowired
    ObjectFinal objectFinal;

    public GetOrgsOutput GetDataUsers(GetOrgsInput input) {
        GetOrgsOutput output = new GetOrgsOutput();
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 获取部门信息
     *  时间: 2019/4/26 13:56 
     *  参数: 
     */
    public GetOrgsOutput GetOrgs(GetOrgsInput input) throws Exception {
        GetOrgsOutput output = new GetOrgsOutput();
        input.setVersionNo(objectFinal.getDataEffectiveVersion());
        List<Orgs> orgs=getOrgs(null, input.getAppCode(), false, input.getVersionNo(),false);

        List<OrgDto> orgDtos = GetOrgTreeList(orgs);
        output.setOrgs(orgDtos);
        BusinessLogger.Info(LoggerGenre.View, "获取部门信息", input);
        return output;
    }


 //   @Cacheable(value="orgs", keyGenerator = "firstParamKeyGenerator")
//    @CachePut(key = "#p0")
//    @CacheEvict(key ="#p0",allEntries=true)
    /**
     *  创建人:Coke
     *  备注: 部门信息转换树形结构
     *  时间: 2019/4/26 13:56 
     *  参数: 
     */
    public List<OrgDto> GetOrgTreeList(List<Orgs> orgs) throws Exception {
        List<OrgDto> orgDtos = new ArrayList<OrgDto>();
        Map<String, OrgDto> parentOrgDtoDic = new HashMap<String, OrgDto>();
        if (orgs != null && orgs.size() > 0) {
            for (int i = 0; i < orgs.size(); i++) {
                OrgDto orgDto = new OrgDto();
                //ObjectUnit.Copy(orgs.get(i), orgDto);
                BeanUtils.copyProperties(orgs.get(i), orgDto);
                orgDto.setKey(orgs.get(i).getId());
                orgDto.setValue(orgs.get(i).getId());
                orgDto.setTitle(orgs.get(i).getOrgName());
                if (orgs.get(i).getParentId() == null || !parentOrgDtoDic.containsKey(orgs.get(i).getParentId())) {
                    orgDtos.add(orgDto);
                } else {
                    OrgDto parentOrgDto = parentOrgDtoDic.get(orgDto.getParentId());
                    if (parentOrgDto.getChildren() == null) {
                        parentOrgDto.setChildren(new ArrayList<OrgDto>());
                    }
                    parentOrgDto.getChildren().add(orgDto);
                }
                parentOrgDtoDic.put(orgDto.getId(), orgDto);
            }
        }
        return orgDtos;
    }

    /**
     *  创建人:Coke
     *  备注: 获取部门基础数据
     *  时间: 2019/4/26 14:06
     *  参数: 
     */
//    @Cacheable(value="basedata")
    public List<Orgs> getOrgs(String parentId, String AppCode, boolean withParent, Long versionNo, boolean jos){
        if (!jos) {
            List<Orgs> orgs = orgsMapper.getOrgs(parentId,AppCode,versionNo);
            return orgs;
        }else{
            List<Orgs> orgs = orgsMapper.getOrgJobs(parentId,AppCode,versionNo);
            return orgs;
        }
    }

}
